<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar title="登录">
      <i slot="left" class="iconfont iconguanbi" @click="$router.go(-1)"></i>
    </van-nav-bar>

    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginFormRef">
      <van-field
        name="mobile"
        v-model="user.mobile"
        placeholder="请输入手机号"
        :rules="loginRules.mobile"
        maxlength="11"
        type="number"
      >
        <i slot="left-icon" class="iconfont iconshouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="loginRules.code"
        maxlength="6"
        type="number"
      >
        <i slot="left-icon" class="iconfont iconyanzhengma"></i>
        <template #button>
          <div class="box">
            <van-count-down
              v-if="timeout"
              :time="60000"
              format="sss后再试"
              @finish="timeout = false"
            />
            <van-button
              v-else
              class="sendma"
              size="small"
              @click.prevent="sendSms"
              >发送验证码</van-button
            >
          </div>
        </template>
      </van-field>
      <div class="login-button">
        <van-button block type="info" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
// 导入请求模块
import { login, sendSms } from '@/api/user'

export default {
  name: 'LoginPage',
  data () {
    return {
      user: {
        mobile: '13955645801',
        code: '246810'
      },
      loginRules: {
        mobile: [
          {
            pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
            message: '手机号码格式不正确！'
          }
        ],
        code: [
          {
            pattern: /^\d{6}$/,
            message: '验证格式不正确！'
          }
        ]
      },
      timeout: false
    }
  },
  methods: {
    // 表单提交按钮事件
    async onSubmit () {
      this.$toast.loading({
        // 登陆提示
        message: '登录中...',
        forbidClick: true, // 禁止背景点击
        duration: 0 // 展示时长(ms)，值为 0 时，toast 不会消失
      })

      // 发送登录请求
      try {
        const res = await login(this.user)
        this.$toast.success('登录成功！')
        this.$router.push('/')
        this.$store.commit('setUser', res.data.data)
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或者验证码错误！')
        } else {
          this.$toast.fail('未知错误，请稍后再试!')
        }
      }
    },
    // 发送短信验证码事件
    async sendSms () {
      try {
        await this.$refs.loginFormRef.validate('mobile')
      } catch (error) {
        return this.$toast.fail('手机号码格式不正确！')
      }
      this.timeout = true
      // 发送验证码请求
      try {
        await sendSms(this.user.mobile)
      } catch (error) {
        if (error.response.status === 429) {
          this.timeout = false
          return this.$toast('操作频繁，请稍后再试！')
        } else {
          this.$toast('发送失败，请稍后再试！')
        }
      }
    }
  }
}
</script>

<style scoped>
i {
  font-size: 37px;
}
.van-button {
  border-radius: 10px;
  background-color: #6db4fb;
  border: none;
}
.login-button {
  margin: 53px 33px;
}
.sendma,
.box,
.van-count-down {
  width: 152px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  color: #666;
  font-size: 22px;
  border-radius: 23px;
  text-align: center;
  vertical-align: middle;
  padding: 0;
}
</style>
